package com.may.wlrj.sourceafis;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.PostConstruct;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.machinezoo.sourceafis.FingerprintImage;
import com.machinezoo.sourceafis.FingerprintMatcher;
import com.machinezoo.sourceafis.FingerprintTemplate;
import com.may.wlrj.dao.UserDAO;
import com.may.wlrj.entity.FingerEntity;



@Component
public class AFISUtil {
	public static AFISUtil afisUtil;
	private double threshold = 40;
	private static final ConcurrentHashMap<String,FingerprintTemplate> userDetails;
	public final double dpi = 363; 
	private UserDAO userDao;
	@Autowired
    public void setUserDao(UserDAO userDao){
        this.userDao =userDao;
    }
    static {
    	userDetails = new ConcurrentHashMap<String,FingerprintTemplate>();
    }
	@PostConstruct
    public void init() {    
		afisUtil = this;
    } 
	
	//初始化指纹数据,保存到内存中方便比对识别
	public void initFingerDetails() {
		System.out.println("--------------->>初始化指纹库");
		System.out.println("Before allocate map, free memory is " + Runtime.getRuntime().freeMemory()/(1024*1024) + "M");
		List<FingerEntity> fingers = userDao.findFingers();
		if(fingers != null) {
			for(FingerEntity finger : fingers) {
				
				userDetails.put(finger.getUsername()+"-"+finger.getFingerflag(), new FingerprintTemplate(
					    new FingerprintImage()
				        .dpi(dpi)
				        .decode(base64String2ByteFun(finger.getFingerbase()))));
			}
			
		}
		System.out.println("After allocate map, free memory is " + Runtime.getRuntime().freeMemory()/(1024*1024) + "M");
		System.out.println("--------------->>初始化指纹库完成:" );
	}
	
	public void AdduserDetail(String key,String fingerbase) {
		userDetails.put(key, new FingerprintTemplate(
			    new FingerprintImage()
		        .dpi(dpi)
		        .decode(base64String2ByteFun(fingerbase))));
	}
	
	public void DeluserDetail(String key) {
		userDetails.remove(key);
	}
	
	public boolean CompareFinger(byte[] probeImage ,byte[] candidateImage) {
		FingerprintTemplate probe = new FingerprintTemplate(
			    new FingerprintImage()
			        .dpi(dpi)
			        .decode(probeImage));

		FingerprintTemplate candidate = new FingerprintTemplate(
		    new FingerprintImage()
		        .dpi(dpi)
		        .decode(candidateImage));
		
		double score = new FingerprintMatcher()
			    .index(probe)
			    .match(candidate);
		
		boolean matches = score >= threshold;
		System.out.println("----score==" + score);
		return matches;
	}
	
	public String find(FingerprintTemplate probe) {
	    FingerprintMatcher matcher = new FingerprintMatcher()
	        .index(probe);
	    String match = "";
	    double high = 0;
	    Iterator<Map.Entry<String, FingerprintTemplate>> entries = userDetails.entrySet().iterator();
	    while (entries.hasNext()) {
	    	 
	    	Map.Entry<String, FingerprintTemplate> entry = entries.next();
	    	double score = matcher.match(entry.getValue());
	        if (score > high) {
	            high = score;
	            match = entry.getKey();
	        }
	    }
	    return high >= threshold ? match : "";
	}
	
	public static byte[] base64String2ByteFun(String base64Str){
        return Base64.decodeBase64(base64Str);
    }
    //byte[]转base64
    public static String byte2Base64StringFun(byte[] b){
        return Base64.encodeBase64String(b);
    }
}
